package surveybrain.common.spellcheck
{
   import mx.core.Container;
   import mx.controls.TextInput;
   import mx.controls.TextArea;
   import flash.events.Event;
   
   public class SpellCheckUtils
   {
      
     public static function populateTextArray(container : Container, textArray : Array, textCtrlsArray : Array, includeNonVisibleCtrls : Boolean = false) : void
     {
        
        //mcn 1/3/08: checking container height below in addition to visible since visible
        //is very unreliable.  This check was put in here to avoid spell checking hidden
        //PromptAnswerInput components.
        if ( !includeNonVisibleCtrls && !(container.visible && container.height > 0) )
            return;
        
        
        for ( var c : int = 0; c < container.numChildren; c++ )
        {
           if ( container.getChildAt(c) is Container )
           {
              populateTextArray(container.getChildAt(c) as Container, textArray, textCtrlsArray);
           }
           else if ( container.getChildAt(c) is TextInput && 
                     (container.getChildAt(c) as TextInput).id != "pw" &&
                     ( includeNonVisibleCtrls || (container.getChildAt(c) as TextInput).visible)
                   )
           {
              textCtrlsArray.push( container.getChildAt(c) );  
              textArray.push( (container.getChildAt(c) as TextInput).text );  
           }
           else if ( container.getChildAt(c) is TextArea  &&
                     ( includeNonVisibleCtrls || (container.getChildAt(c) as TextArea).visible )
                   )
           {
              textCtrlsArray.push( container.getChildAt(c) );                    
              textArray.push( (container.getChildAt(c) as TextArea).text );  
           }
        }
     }         
      
     
     public static function updateTextCtrls(srcArray:Array, textCtrlsArray : Array):void
     {
        for ( var i : int = 0; i < textCtrlsArray.length; i++ )
        {
           if ( textCtrlsArray[i] is TextInput )
           {
              if ( (textCtrlsArray[i] as TextInput).text != srcArray[i] )
              {
                 (textCtrlsArray[i] as TextInput).text = srcArray[i];
                 (textCtrlsArray[i] as TextInput).dispatchEvent( new Event(flash.events.Event.CHANGE) );                  
              }   
           }
           else   
           {
              if ( (textCtrlsArray[i] as TextArea).text != srcArray[i] )
              {
                 (textCtrlsArray[i] as TextArea).text = srcArray[i];                  
                 (textCtrlsArray[i] as TextArea).dispatchEvent( new Event(flash.events.Event.CHANGE) );
              }   
              
           }   
        }
     }
      
      
   }
}